package com.huey.utils.annotation.filed;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * @author xubo
 * @date 2022/7/12 14:46
 */
public class VerificationField implements ConstraintValidator<CheckField, Object> {
    /**
     * 从注解中获取合法的参数值
     */
    private List<String> paramValues;

    @Override
    public void initialize(CheckField checkField) {
        //初始化时获取注解上的值
        paramValues = Arrays.asList(checkField.fieldValues());
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext context) {
        if (paramValues.contains(o)) {
            return true;
        }
        //自定义的参数列表中不存在
        return false;
    }
}
